package web.mapper;

import web.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {
    /*
     * 分页查询数据
     *
     * @param offset 偏移量
     *
     * @param limit 数据限制量
     */
    @Select("select id,`name`,`disabled`, `deleted` from `user` where deleted=0 LIMIT #{o},#{l}")
    public List<User> find(@Param("o") int offset, @Param("l") int limit);

    /*
     * 查询总数
     */
    @Select("select count(1) from `user` where deleted=0")
    public Long count();

    // 1.查询账号是否存在
    // select count(1) from `user` where username = 'Tom1'
    @Select("select count(1) from `user` where username = #{u}")
    public boolean exists(@Param("u") String username);

    @Select("select count(1) from `user` where id = #{id}")
    public boolean existsById(@Param("id") Long id);

    // 2.根据账号和密码查找id
    // select id from `user` where username='Tom1' and `password`=md5('123')
    @Select("select id from `user` where username=#{u}  and `password`=md5(#{p})")
    public Long findIdUsernameAndPassword(@Param("u") String username, @Param("p") String password);

    // 3.账号是否已经被删除
    // select count(1) from `user` where id = 2 and deleted = 1
    @Select("select count(1) from `user` where id = #{i} and deleted = 1")
    public boolean isDeleted(@Param("i") Long id);

    // 4.账号是否已经被禁用
    // select count(1) from `user` where id = 2 and disabled = 1
    @Select("select count(1) from `user` where id = #{i} and disabled = 1")
    public boolean isDisabled(@Param("i") Long id);

    // 新增帐号
    @Select("insert into user(`username`, `name`) values(#{u}, #{n})")
    public void addUser(@Param("u") String username, @Param("n") String name);

    // 修改帐号
    @Select("update user set password = md5(#{p}) where username = #{u}")
    public boolean updateUser(@Param("u") String username, @Param("p") String password);

    // 根据id更新帐号和密码
    @Select("update user set password = md5(#{p}), name = #{n} where id = #{id}")
    public void updateUserNameAndPassword(@Param("id") Long id, @Param("n") String name,
            @Param("p") String password);

    @Select("update user set deleted = 1 where id = #{id}")
    public void deleteUser(@Param("id") Long id);

    @Select("update user set disabled = 1 where id = #{id}")
    public void disableUser(@Param("id") Long id);

    @Select("update user set disabled = 0 where id = #{id}")
    public void enableUser(@Param("id") Long id);

    @Select("select username, name, id, disabled, deleted from user where id = #{id}")
    public User userInfo(@Param("id") Long id);

}
